package git.soulbgm;

import git.soulbgm.pojo.MapPoint;
import git.soulbgm.utils.GraphUtils;
import git.soulbgm.utils.PropertiesUtil;

import java.io.*;
import java.util.*;

/**
 * 程序入口
 *
 * @author SoulBGM
 * @date 2022/10/18 09:47
 */
public class Application {

    private final static String BOUNDARY_COORDINATES_BASE_PATH = "administrative_divisions_boundary_coordinates/";

    public static void main(String[] args) throws IOException {
        Map<String, MapPoint[]> mapPointMap = getBoundaryCoordinatesMap();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String next = scanner.nextLine();
            if ("exit".equals(next)) {
                break;
            } else {
                System.out.println("输入城市Code：");
                String code = scanner.nextLine();
                System.out.println("输入经度：");
                double lng = Double.parseDouble(scanner.nextLine());
                System.out.println("输入纬度：");
                double lat = Double.parseDouble(scanner.nextLine());
                boolean result = GraphUtils.isPointInPolygon(lng, lat, mapPointMap.get(code));
                System.out.println("是否在范围内：" + (result ? "在" : "不在"));
            }
        }
    }

    /**
     * 获得到行政区划边界坐标
     *
     * @return {@link Map}<{@link String}, {@link MapPoint[]}>
     * @throws IOException ioexception
     */
    public static Map<String, MapPoint[]> getBoundaryCoordinatesMap() throws IOException {
        Map<String, String> map = PropertiesUtil.getFolderAll(BOUNDARY_COORDINATES_BASE_PATH);
        Map<String, MapPoint[]> mapPointMap = new HashMap<>();
        Set<String> keySet = map.keySet();
        for (String k : keySet) {
            String[] pointArray = map.get(k).split(";");
            MapPoint[] mapPointArray = new MapPoint[pointArray.length];
            for (int i = 0; i < pointArray.length; i++) {
                String p = pointArray[i];
                String[] split = p.split("\\|")[0].split(",");
                mapPointArray[i] = new MapPoint(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
            }
            mapPointMap.put(k.substring(0, k.length() - 4), mapPointArray);
        }
        return mapPointMap;
    }

}
